package Initialize

import (
	"fmt"
	"gorm-gin-master/Global"
	"gorm-gin-master/Models"
	"gorm.io/gorm"
	"gorm.io/driver/mysql"
)

func InitMysqlDB() {
	mysqlInfo := Global.Settings.Mysqlinfo
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlInfo.Name, mysqlInfo.Password, mysqlInfo.Host,
		mysqlInfo.Port, mysqlInfo.DBName)
	Global.DB, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	Global.DB.AutoMigrate(&Models.Book{});
}
